package org.zjt.config;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.zjt.register.ZKRouteMonitor;
import org.zjt.register.service.IRegisterService;
import org.zjt.register.service.impl.RegisterServiceImpl;
import org.zjt.util.RpcContext;

/**
 * Created by Administrator on 2017/4/29.
 *
 * config中初始化配置类，不能new出来，只能EnableConfigurationProperties
 *
 */
@Configurable
@EnableConfigurationProperties(ApplicationProperties.class)
public class StarterConfig {

    @Autowired
    ApplicationProperties config ;

    @Bean
    public ZKConnectCenter zkConnectCenter()throws Exception{
        ZKConnectCenter connectCenter = new ZKConnectCenter(config.getRegisterConfig());
        return connectCenter;
    }

    @Bean
    public RpcDiHandler rpcDiHandler(){
        RpcContext.initLoadBalance(config.getLoadBalance());
        return new RpcDiHandler();
    }


    @Bean
    public IRegisterService registerService(){
        return new RegisterServiceImpl();
    }


    @Bean
    public ZKRouteMonitor zKRouteMonitor(){
        return new ZKRouteMonitor();
    }




    public ApplicationProperties getConfig() {
        return config;
    }

    public void setConfig(ApplicationProperties config) {
        this.config = config;
    }
}
